const core = require('@babel/core')
const traverse = require('@babel/traverse').default
const generate = require('@babel/generator').default

const sourceCode = `
  const hello = () => {}
`

// const myPlugin = {
//   visitor: {
//     Identifier(path) {
//       if (path.node.name === 'hello') {
//         path.node.name = 'world'
//       }
//     },
//   },
// }

// const { code } = core.transform(sourceCode, {
//   plugins: [myPlugin],
// })

const transName = (code) => {
  const ast = core.parse(code)
  traverse(ast, {
    enter(path) {
      if (path.isIdentifier({ name: 'hello' })) {
        path.node.name = 'world'
      }
    },
  })

  return generate(ast).code
}

// console.log(code)
console.log(transName(sourceCode))

// const hello = () => {}
// // 修改函数名
// const world = () => {}
